package com.example.seakdreamandroid;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seakdreamandroid.entity.User;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SetPasswordActivity extends AppCompatActivity {
    private EditText passwordCreate;
    private Button passwordCorrect;
    User user;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_password);
        user = (User) getApplication();
        sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        editor = sharedPreferences.edit();

        passwordCreate = findViewById(R.id.password_create);
        passwordCorrect = findViewById(R.id.password_correct);

        passwordCorrect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = passwordCreate.getText().toString().trim();
                if (TextUtils.isEmpty(password)){
                    Toast.makeText(SetPasswordActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                }else{
                    //保存密码
                    Intent intent = getIntent();
                    String phone = intent.getStringExtra("phone");
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody body = new FormBody.Builder()
                            .add("phone", phone)
                            .add("password", password)
                            .build();
                    Request request = new Request.Builder()
                            .url(Constant.BASE_URL + "UserInfo/createUserInfo")
                            .post(body)
                            .build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            Log.i("okhttp", "请求失败");
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String str = response.body().string();
                            try {
                                JSONObject result = new JSONObject(str);
                                Intent intentToMain = new Intent();
                                intentToMain.setClass(SetPasswordActivity.this, MainActivity.class);
                                JSONObject userInfo = result.getJSONObject("data");
//                                intentToMain.putExtra("userId", userInfo.getString("userId"));
                                user.setUserId(userInfo.getString("userId"));
                                editor.putString("token", result.getString("token"));
                                editor.commit();
                                user.setUserId(userInfo.getString("userId"));
                                startActivity(intentToMain);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });
    }

}